/******************************************************************************
 Copyright (c) 2014-2017 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

 REVISION:      $Revision: #6 $
******************************************************************************/

#include "csr_bt_pac_handler.h"

static void csrBtPacMessagePut(CsrSchedQid phandle, void *msg)
{
    CsrSchedMessagePut(phandle, CSR_BT_PAC_PRIM, msg);
}

static void csrBtPacCopyVersionInfo(PacInst* pInst, CsrBtPbVersionInfo *pVersionInfo)
{
    CsrUint8 size;
    CsrUint8 folderId = pInst->targetFolderId >> 8;

    if ((folderId & CSR_BT_PB_PB_ID)
        || (folderId & CSR_BT_PB_FAV_ID)
        || (folderId & CSR_BT_PB_SPD_ID))
    {
        size = sizeof(CsrBtPbVersionInfo);
    }
    else
    {
        size = sizeof(pVersionInfo->databaseIdentifier)
               + sizeof(pVersionInfo->primaryVersionCounter);
    }

    CsrMemCpy(pVersionInfo, &pInst->versionInfo, size);
}

void CsrBtPacPullPbCfmSend(PacInst *pInst,
                           CsrBtObexResponseCode responseCode)
{
    CsrBtPacPullPbCfm *pMsg = (CsrBtPacPullPbCfm *) CsrPmemZalloc(sizeof(CsrBtPacPullPbCfm));

    pMsg->type = CSR_BT_PAC_PULL_PB_CFM;
    pMsg->newMissedCall = pInst->newMissedCall;
    pMsg->pbSize = pInst->pbSize;
    pMsg->responseCode = responseCode;

    csrBtPacCopyVersionInfo(pInst, &pMsg->versionInfo);
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacPullPbIndSend(CsrSchedQid appHandle,
                           CsrUint16 bodyOffset,
                           CsrUint16 bodyLength,
                           CsrUint8 * obexPacket,
                           CsrUint16 obexPacketLength)
{
    CsrBtPacPullPbInd *pMsg = (CsrBtPacPullPbInd *) CsrPmemAlloc(sizeof(CsrBtPacPullPbInd));
    pMsg->type = CSR_BT_PAC_PULL_PB_IND;
    pMsg->bodyLength = bodyLength;
    pMsg->bodyOffset = bodyOffset;
    pMsg->payload = obexPacket;
    pMsg->payloadLength = obexPacketLength;
    csrBtPacMessagePut(appHandle, pMsg);
}

void CsrBtPacPullvCardEntryCfmSend(PacInst *pInst,
                                   CsrBtObexResponseCode responseCode)
{
    CsrBtPacPullVcardEntryCfm *pMsg = (CsrBtPacPullVcardEntryCfm *)
                    CsrPmemAlloc(sizeof(CsrBtPacPullVcardEntryCfm));
    pMsg->type = CSR_BT_PAC_PULL_VCARD_ENTRY_CFM;
    pMsg->responseCode = responseCode;
    CsrMemCpy(pMsg->databaseId, pInst->versionInfo.databaseIdentifier,
              sizeof(pInst->versionInfo.databaseIdentifier));
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacPullvCardEntryIndSend(CsrSchedQid appHandle,
                                   CsrUint16 bodyOffset,
                                   CsrUint16 bodyLength,
                                   CsrUint8 * obexPacket,
                                   CsrUint16 obexPacketLength)

{
    CsrBtPacPullVcardEntryInd *pMsg = (CsrBtPacPullVcardEntryInd *)
                    CsrPmemAlloc(sizeof(CsrBtPacPullVcardEntryInd));
    pMsg->type = CSR_BT_PAC_PULL_VCARD_ENTRY_IND;
    pMsg->bodyLength = bodyLength;
    pMsg->bodyOffset = bodyOffset;
    pMsg->payload = obexPacket;
    pMsg->payloadLength = obexPacketLength;
    csrBtPacMessagePut(appHandle, pMsg);
}

void CsrBtPacPullvCardListIndSend(CsrSchedQid appHandle,
                                  CsrUint16 bodyOffset,
                                  CsrUint16 bodyLength,
                                  CsrUint8 * obexPacket,
                                  CsrUint16 obexPacketLength)
{
    CsrBtPacPullVcardListInd *pMsg = (CsrBtPacPullVcardListInd *)
                    CsrPmemAlloc(sizeof(CsrBtPacPullVcardListInd));
    pMsg->type = CSR_BT_PAC_PULL_VCARD_LIST_IND;
    pMsg->bodyLength = bodyLength;
    pMsg->bodyOffset = bodyOffset;
    pMsg->payload = obexPacket;
    pMsg->payloadLength = obexPacketLength;
    csrBtPacMessagePut(appHandle, pMsg);
}

void CsrBtPacPullvCardListCfmSend(PacInst *pInst,
                                  CsrBtObexResponseCode responseCode)
{
    CsrBtPacPullVcardListCfm *pMsg = (CsrBtPacPullVcardListCfm *)
                    CsrPmemZalloc(sizeof(CsrBtPacPullVcardListCfm));
    pMsg->type = CSR_BT_PAC_PULL_VCARD_LIST_CFM;
    pMsg->newMissedCall = pInst->newMissedCall;
    pMsg->pbSize = pInst->pbSize;
    csrBtPacCopyVersionInfo(pInst, &pMsg->versionInfo);
    pMsg->responseCode = responseCode;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}


void CsrBtPacSetFolderCfmSend(PacInst* pInst,
                              CsrBtObexResponseCode responseCode)
{
    CsrBtPacSetFolderCfm* pMsg = (CsrBtPacSetFolderCfm*) CsrPmemAlloc(sizeof(CsrBtPacSetFolderCfm));
    pMsg->type = CSR_BT_PAC_SET_FOLDER_CFM;
    pMsg->responseCode = responseCode;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacSetBackFolderCfmSend(PacInst* pInst,
                                  CsrBtObexResponseCode responseCode)
{
    CsrBtPacSetBackFolderCfm* pMsg = (CsrBtPacSetBackFolderCfm*)
                    CsrPmemAlloc(sizeof(CsrBtPacSetBackFolderCfm));
    pMsg->type = CSR_BT_PAC_SET_BACK_FOLDER_CFM;
    pMsg->responseCode = responseCode;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacSetRootFolderCfmSend(PacInst* pInst,
                                  CsrBtObexResponseCode responseCode)
{
    CsrBtPacSetRootFolderCfm* pMsg = (CsrBtPacSetRootFolderCfm*)
                    CsrPmemAlloc(sizeof(CsrBtPacSetRootFolderCfm));
    pMsg->type = CSR_BT_PAC_SET_ROOT_FOLDER_CFM;
    pMsg->responseCode = responseCode;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacAbortCfmSend(PacInst* pInst)
{
    CsrBtPacAbortCfm* pMsg = (CsrBtPacAbortCfm*) CsrPmemAlloc(sizeof(CsrBtPacAbortCfm));
    pMsg->type = CSR_BT_PAC_ABORT_CFM;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

#ifdef INSTALL_PAC_CUSTOM_SECURITY_SETTINGS
void CsrBtPacSecurityOutCfmSend(CsrBtPacSecurityOutReq* pMsg,
                                CsrBtResultCode result)
{
    CsrBtPacSecurityOutCfm* prim;
    prim = (CsrBtPacSecurityOutCfm*) CsrPmemAlloc(sizeof(CsrBtPacSecurityOutCfm));
    prim->type = CSR_BT_PAC_SECURITY_OUT_CFM;
    prim->resultCode = result;
    prim->resultSupplier = CSR_BT_SUPPLIER_OBEX_PROFILES;
    csrBtPacMessagePut(pMsg->appHandle, prim);
}
#endif

void CsrBtPacConnectCfmSend(PacInst* pInst,
                            CsrBtResultCode resultCode,
                            CsrBtSupplier resultSupplier,
                            CsrUint16 maxPeerObexPacketLength,
                            CsrBtConnId cid)
{
    CsrBtPacConnectCfm* pMsg = (CsrBtPacConnectCfm*) CsrPmemAlloc(sizeof(CsrBtPacConnectCfm));
    pMsg->type = CSR_BT_PAC_CONNECT_CFM;
    pMsg->resultCode = resultCode;
    pMsg->resultSupplier = resultSupplier;
    pMsg->obexPeerMaxPacketSize = maxPeerObexPacketLength;
    pMsg->supportedRepositories = pInst->supportedRepositories;
    pMsg->supportedFeatures = pInst->supportedFeatures;
    pMsg->btConnId = cid;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacAuthenticateIndSend(PacInst* pInst,
                                 const CsrBtDeviceAddr* deviceAddr,
                                 CsrUint8 options,
                                 CsrUint16 realmLength,
                                 CsrUint8* realm)
{
    CsrBtPacAuthenticateInd* pMsg = (CsrBtPacAuthenticateInd*)
                    CsrPmemAlloc(sizeof(CsrBtPacAuthenticateInd));
    pMsg->type = CSR_BT_PAC_AUTHENTICATE_IND;
    pMsg->deviceAddr = *deviceAddr;
    pMsg->options = options;
    pMsg->realmLength = realmLength;
    pMsg->realm = realm;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

void CsrBtPacDisconnectIndSend(PacInst* pInst,
                               CsrBtReasonCode reasonCode,
                               CsrBtSupplier reasonSupplier)
{
    CsrBtPacDisconnectInd* pMsg = (CsrBtPacDisconnectInd*)
                    CsrPmemAlloc(sizeof(CsrBtPacDisconnectInd));
    pMsg->type = CSR_BT_PAC_DISCONNECT_IND;
    pMsg->reasonCode = reasonCode;
    pMsg->reasonSupplier = reasonSupplier;
    csrBtPacMessagePut(pInst->appHandle, pMsg);
}

